package com.zuoguang.kidenglish;

import java.util.Random;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class WordsDao {
	private Context context = null;
	private int count=0;

	public WordsDao(Context c) {
		this.context = c;
		
		SQLiteDatabase db = new DatabaseHelper(this.context, "english.db").getWritableDatabase();
		
		Cursor cur = db.rawQuery("SELECT *  FROM Words ORDER BY r_times ASC,err_times DESC",null);
		cur.moveToFirst();
		while (!cur.isAfterLast()) { 
			WordPair wp = new WordPair(cur.getInt(0), cur.getString(1),
					cur.getString(2), cur.getInt(3), cur.getInt(4));
			Log.i("System.out", "Init:"+wp);
			cur.moveToNext(); 
	      } 
		cur.close();
		
		
		cur = db.rawQuery("SELECT count(*) FROM Words",null);
		cur.moveToFirst();
		count = cur.getInt(0);
		cur.close();
		db.close();
	}

	public WordPair getWord() {
		int pos = random()-1;
		Log.i("System.out", "Pos:"+pos);
		SQLiteDatabase db = new DatabaseHelper(this.context, "english.db")
				.getWritableDatabase();
		Cursor cur = db
				.rawQuery(
						"SELECT id,txt_en,txt_zh,r_times,err_times  FROM Words ORDER BY r_times ASC,err_times DESC LIMIT 1 OFFSET ?",
						new String[] { Integer.toString(pos) });
		cur.moveToFirst();
		WordPair wp = new WordPair(cur.getInt(0), cur.getString(1),
				cur.getString(2), cur.getInt(3), cur.getInt(4));
		cur.close();
		db.close();
		Log.i("System.out", "Get:"+wp);
		return wp;
	}

	public void update(WordPair wp) {
		Log.i("System.out", "Update:"+wp);
		SQLiteDatabase db = new DatabaseHelper(this.context, "english.db")
				.getWritableDatabase();
		db.execSQL("update words set r_times=?,err_times=? where id=?;",
				new Object[] { wp.r_times, wp.err_times, wp.id });
		db.close();
	}

	public WordPair next() {
		return getWord();
	}

	public WordPair pre() {
		return getWord();
	}

	public int random() {
		Random r = new Random();
		int which = r.nextInt(100) + 1;
		int rnd,pos;
		if (which <= 60){
			rnd=r.nextInt((int)(count*0.1))+1;
			pos=rnd;
			return pos;
		}
		else if (which > 60 && which <= 85){
			rnd=r.nextInt((int)(count*0.25))+1;
			pos=(int)(count*0.1)+rnd;
			return pos;
		}
		else if (which > 85 && which <= 95){
			rnd=r.nextInt((int)(count*0.55))+1;
			pos=(int)(count*0.35)+rnd;
			return pos;
		}
		else{
			rnd=r.nextInt((int)(count*0.1))+1;
			pos=(int)(count*0.9)+rnd;
			return pos;
		}
	}
}

class WordPair {
	public String en, zh;
	public int id, r_times, err_times,click_times;

	public WordPair(int id, String en, String zh, int r_times, int err_times) {
		this.id = id;
		this.en = en;
		this.zh = zh;
		this.r_times = r_times;
		this.err_times = err_times;
		click_times=0;
	}
	
	public void finish(WordsDao dao){
		if(click_times>0){
			err_times=err_times+click_times;
			r_times=(int)(r_times*0.33);
		}
		else{
			r_times++;
			err_times=(int)(err_times*0.66);
		}
		dao.update(this);
	}
	
	public void error(){
		click_times++;
	}

	@Override
	public String toString() {
		return "WordPair [id="+id+", en=" + en + ", zh=" + zh +  ", rt=" + r_times + ", et=" + err_times +"]";
	}
}